Ext.Loader.setPath("FormFieldUtil",contextPath+"ui_tpl/FormFieldUtil.js");
Ext.define("Ext.ux.SingleFieldSet",{
		extend:'Ext.form.Panel',
		xtype:'singlefieldset',
		bodyPadding: 15,
		scrollable:true,
		requires:[
			'FormFieldUtil'
		],
		layout: 'anchor',
		defaults: {
		        anchor: '100%'
		 },
		defaultType: 'textfield',
		initComponent:function(){
			var me=this,metaData=this.metaData,
			refMetaMap=metaData["refMetaMap"],
			refEnumMap=metaData["refEnumMap"],
			mainMetaId=metaData['id'],
			mainMeta=this.mainMeta=refMetaMap[mainMetaId],
			attrs=this.attrs=mainMeta["attrs"],
			loadRecordUrl=attrs["loadRecordUrl"]||(contextPath+"meta/singleRecord/"+metaData["id"]),
			initConfig=me.initialConfig;
			this.idProperty=this.attrs["idProperty"];
			this.action=this.attrs["action"];
			this.url=this.action["url"];
			me.initButtons();
			var items=this.items=[],cs=mainMeta["children"];
			Ext.iterate(cs,function(modelId,i){
				var refModel=refMetaMap[modelId],sfields=refModel["fields"],
				fieldset={
						xtype:'fieldset',
						width:'auto',
						layout:'vbox',
						title:refModel["text"]
				};
				fieldset["items"]=FormFieldUtil.getFormFields(modelId,metaData);
				items.push(fieldset);
			});
			this.callParent();
		},
		listeners:{
			'afterrender':function(){
				var me=this,params={};
				if(globalId){
					params={id:globalId};
				}
				Ext.Ajax.request({
					url:loadRecordUrl,
					method:"GET",
					params:params,
					success: function(response, opts) {
				         var obj = Ext.decode(response.responseText);
				         if(obj["success"]){
				        	 var data=obj["data"];
				        	 var rec=Ext.create(modelName,data);
				        	 me.loadRecord(rec);
				         }
				     },

				     failure: function(response, opts) {
				    	 Ext.toast('提示','数据加载失败');
				     }
				});
			}
		},
		initButtons:function(){
			this.buttons=[{
		        text: '重置',
		        iconCls:"fa fa-refresh fa-fw",
		        handler: function() {
		            this.up('form').getForm().reset();
		        }
		    }, {
		        text: '提交',
		        iconCls:"fa fa-arrow-up",
		        formBind: true,
		        disabled: true,
		        handler: function() {
		            var form = this.up('form').getForm();
		            if (form.isValid()) {
		                form.submit({
		                    success: function(form, action) {
		                    	Ext.toast("提交成功",'提示', 't');
		                    	
		                    },
		                    failure: function(form, action) {
		                    	Ext.toast(action.result.message,'提示', 't');
		                    }
		                });
		            }
		        }
		    }];
		}
		
	});



